package com.cskaoyan.aspect;

import com.cskaoyan.bean.ActiveUser;
import com.cskaoyan.bo.system.LogInsertBo;
import com.cskaoyan.bo.user.UserLoginBo;
import com.cskaoyan.service.system.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class LogAspect {
    private String username;

    @Autowired
    LogService logService;

    // @Pointcut("execution(* com.cskaoyan.controller..*(..))")
    @Pointcut("@annotation(com.cskaoyan.anno.MyLog)")
    public void mypointcut() {
    }

    @Around("mypointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        // 执行委托类的方法
        Object proceed = joinPoint.proceed();

        LogInsertBo logInsertBo = getLogInsertBo();

        if (logInsertBo.getStatus() == null)
            logInsertBo.setStatus(1);

        logService.insertLog(logInsertBo);

        return proceed;
    }

    // 操作出现异常
    @AfterThrowing(value = "mypointcut()", throwing = "exception")
    public void afterThrowing(Exception exception) {

        LogInsertBo logInsertBo = getLogInsertBo();
        logInsertBo.setStatus(0);
        logService.insertLog(logInsertBo);
        System.out.println("afterReturning：" + exception);
    }

    private LogInsertBo getLogInsertBo() {
        LogInsertBo logInsertBo = new LogInsertBo();
        Subject subject = SecurityUtils.getSubject();
        // ActiveUser activeUser = (ActiveUser) subject.getPrincipal();

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String remoteAddr = request.getRemoteAddr();
        String requestURI = request.getRequestURI();

        Integer type = 1;
        String action = null;
        if (requestURI != null && !"".equals(requestURI)) {
            if (requestURI.contains("admin/auth/login")) {
                username = (String) request.getSession().getAttribute("username");
                action = "登陆";
                if (username == null) {
                    logInsertBo.setAdmin("匿名用户");
                    logInsertBo.setStatus(0);
                }
            } else if (requestURI.contains("admin/auth/logout")) {
                action = "登出";
            } else if (requestURI.contains("admin/admin/create")) {
                action = "新增管理员";
            } else if (requestURI.contains("admin/admin/update")) {
                action = "修改管理员";
            } else if (requestURI.contains("admin/admin/delete")) {
                action = "删除管理员";
            }
        }
        // logInsertBo.setAdmin(activeUser.getUsername());
        if (username != null)
            logInsertBo.setAdmin(username);
        logInsertBo.setIp(remoteAddr);
        logInsertBo.setType(type);
        logInsertBo.setAction(action);
        return logInsertBo;
    }

    // @After("mypointcut()")
    // public void after(JoinPoint joinPoint) { // 👉 after
    // Object[] args = joinPoint.getArgs();
    // UserLoginBo userLoginBo = null;
    //
    // if (args != null && !"".equals(args)) {
    //     userLoginBo = (UserLoginBo) args[0];
    // }
    //
    // Signature signature = joinPoint.getSignature();
    // String methodName = signature.getName();
    // if (methodName.toLowerCase().contains("login")) {
    //     System.out.println("login");
    // }
    // }


}
